import initCalendar from '../../components/calendar/index';
const app = getApp()

Page({
  data: {
    monthDataList: [],
    dateDataList: [],
    accountDays: 0,
    accountMoney: 0,
    isShowDialog: false,
    isShowAdd: false
  },
  customData: {
    dataList: [],  // 当前所有缓存数据
    index: '',   //当前编辑的项目索引
    name: '',
    money: '',
    curMonth: '',  // 当前所选月份
    curDate: ''     // 当前多选日期
  },
  onLoad(options) {
    this.initData()
  },
  onShow: function() {
    const conf_m = {
      multi: false, // 是否开启多选,
      disablePastDay: false, // 是否禁选过去的日期
      afterCalendarRender() {},
    }
    conf_m.afterTapDay = (now, all) => {
      this.customData.curDate = now.day
      let _dateData = this.data.monthDataList[now.day]
      let _isshow = _dateData && _dateData.length?true:false
      this.setData({
        isShowDialog: _isshow,
        dateDataList: _dateData || []
      })
    }
    initCalendar(conf_m);
  },
  // 初始化数据
  initData(){
    let that = this
    app.EventBus.$on('curMonth', this, res => {
      console.log('当前月份', res)
      let _curMonth = res.curMonth?res.curMonth:res
      let _curDate = res.curDate?res.curDate:''
      that.customData.curMonth = _curMonth
      that.customData.curDate = _curDate
      let _day = 0, _account = 0
      app.util.getStorageSync('list', data => {
        data.forEach(item => {
          if(item && item.length) {
            _day++
            item.forEach(data => {
              if(data && data.length) {
                data.forEach(mon => {
                  _account += Number(mon.money)
                })
              }
            })
          }
        })
        let _monData = data[_curMonth] || []
        let _dayData = _monData[_curDate] || []
        that.customData.dataList = data || []
        that.setData({
          monthDataList: _monData,
          dateDataList: _dayData,
          accountDays: _day,
          accountMoney: _account
        })
        console.log('当月记录', this.data.monthDataList)
        console.log('当日记录', this.data.dateDataList)
      })
    })

  },
  // 编辑名称
  changeName(e){
    this.customData.name = e.detail.name
    this.customData.index = e.detail.index
  },
  // 编辑金额
  changeMoney(e){
    this.customData.money = e.detail.money
    this.customData.index = e.detail.index
  },
  // 保存记录
  saveCount (e) {
    let that = this
    let _list = {}
    let list = []
    let { monthDataList, dateDataList, accountDays, accountMoney, isShowAdd } = that.data
    if((that.customData.name.trim() == '' || that.customData.money.trim() == '') && isShowAdd) {
      app.util.showToast('内容未填写完整', 'none', 1500)
      return
    }
    if(that.customData.index != '') {
      dateDataList[that.customData.index].name = that.customData.name
      dateDataList[that.customData.index].money = that.customData.money
    } else {
      _list.name = that.customData.name
      _list.money = that.customData.money
      dateDataList.unshift(_list)
    }
    monthDataList[that.customData.curDate] = dateDataList
    that.customData.dataList[that.customData.curMonth] = monthDataList
    monthDataList.forEach(item => {
      if(item && item.length) {
        accountDays++
        item.forEach(mon => {
          accountMoney += Number(mon.money)
        })
      }
    })
    console.log(that.customData.dataList)
    app.util.setStorageSync('list', that.customData.dataList, res => {
      app.util.showToast('保存成功', 'success', 1500)
      that.customData.index = ''
      that.customData.name = ''
      that.customData.money = ''
      that.setData({
        dateDataList,
        monthDataList,
        isShowAdd: false,
        isShowDialog: false,
        accountDays,
        accountMoney
      })
    })

  },
  // 打开弹窗，添加新项
  addNewCount () {
    let { dateDataList, isShowAdd } = this.data
    isShowAdd = !dateDataList.length?true:false
    this.setData({
      isShowDialog: true,
      isShowAdd
    })
  },
  // 删除单条记录
  removeItem(e){
    console.log('当前条数', e.detail)
    let that = this
    let _index = e.detail
    let { monthDataList, dateDataList, accountDays, accountMoney } = that.data
    if(!_index) {
      return
    }
    monthDataList[that.customData.curMonth][that.customData.curDate].splice(_index, 1)
    dateDataList.splice(_index, 1)
    accountMoney = accountMoney - Number(dateDataList[_index].money)
    that.customData.dataList[that.customData.curMonth][that.customData.curDate].splice(_index, 1)
    console.log(that.customData.dataList)
    app.util.setStorageSync('list', that.customData.dateDataList, res => {
      this.setData({
        monthDataList,
        dateDataList,
        accountMoney
      })
      app.util.showToast('已删除', 'none', 1000)
    })
  },
  onUnload () {
    app.EventBus.$remove('curMonth', this)
  }
});